package category

import (
	"context"
	"github.com/gin-gonic/gin"
	grpcCategory "go-gin-api/app/proto/category"
	"go-gin-api/app/request"
	"go-gin-api/app/util/response"
	"log"
	"net/http"
)

// category 标签
type Category struct {
	categoryClient grpcCategory.CategoryService
}

func NewCategory(categoryClient grpcCategory.CategoryService) *Category {
	return &Category{
		categoryClient: categoryClient,
	}
}

// 新增 Add
func (t *Category) Add(c *gin.Context) {
	var (
		categoryRequest grpcCategory.CategoryRequest
		req             request.Category
	)
	// 参数绑定
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest, -1, err.Error(), nil)
		return
	}
	categoryRequest.Name = req.Name
	categoryRequest.Pid = req.Pid
	categoryRequest.SortOrder = req.SortOrder
	res, err := t.categoryClient.Category(context.Background(), &categoryRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest, -1, err.Error(), nil)
		return
	}
	if !res.Res {
		response.ReturnJson(c, http.StatusBadRequest, -1, "error", nil)
		return
	}
	response.ReturnJson(c, http.StatusOK, 1, "success", nil)
}

// 编辑 Edit
func (t *Category) Edit(c *gin.Context) {
	var (
		categoryUpdateRequest grpcCategory.CategoryUpdateRequest
		req                   request.CategoryUpdate
	)
	// 参数绑定
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest, -1, err.Error(), nil)
		return
	}
	categoryUpdateRequest.Id = req.Id
	categoryUpdateRequest.Name = req.Name
	categoryUpdateRequest.SortOrder = req.SortOrder
	categoryUpdateRequest.Pid = req.Pid
	categoryUpdateRequest.Status = req.Status
	res, err := t.categoryClient.CategoryUpdate(context.Background(), &categoryUpdateRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest, -1, err.Error(), nil)
		return
	}
	if !res.Res {
		response.ReturnJson(c, http.StatusBadRequest, -1, "error", nil)
		return
	}
	response.ReturnJson(c, http.StatusOK, 1, "success", nil)
}

// 删除 Delete
func (t *Category) Delete(c *gin.Context) {
	var (
		categoryDelRequest grpcCategory.CategoryDelRequest
		req                request.CategoryDel
	)
	// 参数绑定
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest, -1, err.Error(), nil)
		return
	}
	categoryDelRequest.Id = req.Id
	res, err := t.categoryClient.CategoryDel(context.Background(), &categoryDelRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest, -1, err.Error(), nil)
		return
	}
	if !res.Res {
		response.ReturnJson(c, http.StatusBadRequest, -1, "error", nil)
		return
	}
	response.ReturnJson(c, http.StatusOK, 1, "success", nil)
}

// 详情 Info
func (t *Category) Info(c *gin.Context) {
	var (
		categoryInfoRequest grpcCategory.CategoryInfoRequest
		req                 request.CategoryInfo
	)
	// 参数绑定
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest, -1, err.Error(), nil)
		return
	}
	categoryInfoRequest.Id = req.Id
	res, err := t.categoryClient.CategoryInfo(context.Background(), &categoryInfoRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest, -1, err.Error(), nil)
		return
	}
	response.ReturnJson(c, http.StatusOK, 1, "success", res)
}

// 列表 List
func (t *Category) List(c *gin.Context) {
	var (
		categoryListRequest grpcCategory.CategoryListRequest
		req                 request.CategoryList
	)
	log.Println(req)
	// 参数绑定
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest, -1, err.Error(), nil)
		return
	}
	categoryListRequest.Status = req.Status
	categoryListRequest.Pid = req.Pid
	categoryListRequest.Limit = req.Limit
	categoryListRequest.Page = req.Page
	categoryListRequest.Name = req.Name
	categoryListRequest.CreatedAt = req.CreatedAt
	res, err := t.categoryClient.CategoryList(context.Background(), &categoryListRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest, -1, err.Error(), nil)
		return
	}
	response.ReturnJson(c, http.StatusOK, 1, "success", res)
}
